package com.ruiysoft.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.crm.mapper.BizBookingClientMapper;
import com.ruiysoft.crm.mapper.SysUserMapper;
import com.ruiysoft.crm.model.dto.BizBookingClientDTO;
import com.ruiysoft.crm.model.entity.BizBookingClient;
import com.ruiysoft.crm.model.entity.SysUser;
import com.ruiysoft.crm.service.BizBookingClientService;
import com.ruiysoft.crm.service.SysUserService;
import com.ruiysoft.crm.util.LocalDateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fanyl
 */
@Service
public class BizBookingClientServiceImpl extends ServiceImpl<BizBookingClientMapper, BizBookingClient> implements BizBookingClientService {
    @Autowired
    private BizBookingClientMapper bizBookingClientMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserService sysUserService;

    @Override
    public Object addBookingByClientId(int client, LocalDateTime bookingTime, int userId) {

        SysUser sysUser = sysUserService.getById(userId);

        bizBookingClientMapper.changeStatusByClientId(client);
        BizBookingClient bookingClient = new BizBookingClient();
        bookingClient.setBookingTime(bookingTime);
        bookingClient.setClientId(client);
        bookingClient.setEnterpriseId(sysUser.getEnterpriseId());
        bookingClient.setReadStatus("0");//已读状态
//        bookingClient.setCarNo(client.getCarNo());
//        bookingClient.setCarOwner(client.getCarOwner());
//        bookingClient.setMobile(client.getMobilePhone());
//        //bookingClient.setCarInsuranceDeadline(client.getcar);//车险到期日
        bookingClient.setUserName(sysUser.getUsername());
        bookingClient.setUserId(userId);
        bookingClient.setCreateDate(LocalDateTime.now());
        bookingClient.setModificationDate(LocalDateTime.now());

        bizBookingClientMapper.insert(bookingClient);
        return bookingClient;
    }

    @Override
    public Object changeStatusById(String id) {
        bizBookingClientMapper.changeStatusById(id);
        return null;
    }

    @Override
    public Object getUnReadList(int userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("bookingtime", LocalDateTime.now());
        params.put("user_id", userId);
        params.put("read_Status", "0");
        return bizBookingClientMapper.queryUnReadBooking(params);
        //return bizBookingClientMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BizBookingClientDTO> queryBookingInfo(IPage page, Map<String, Object> params) {
        params.remove("page");
        params.remove("pageSize");
        params.remove("current");
        String bookingTime = (String) params.get("bookingTime");
        String selectType = (String) params.get("selectType");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (bookingTime != null) {
            LocalDateTime startDate;
            LocalDate endDate;
            String start = "";
            String end = "";
            if ("1".equals(selectType)) {//加个注释就是就是
                //  endDate= LocalDateTime.parse(bookingTime,df).toLocalDate();
                endDate = LocalDateTime.parse(bookingTime, df).toLocalDate();
                start = endDate + " 00:00:00";
                end = endDate + " 23:59:59";
            } else {
                // startDate=LocalDateTime.parse("1970-01-01 00:00:00",df);
                endDate = LocalDateTime.parse(bookingTime, df).toLocalDate();
                start = "1970-01-01 00:00:00";
                end = endDate + " 23:59:59";
            }
            params.put("booking.bookingTime", "booking.booking_time BETWEEN '" + start + "' and '" + end + "'");
        }
        params.remove("bookingTime");
        params.remove("selectType");
        List<BizBookingClientDTO> ls = bizBookingClientMapper.queryBookingInfo(page, params);
        page.setRecords(ls);
        return page;
    }

    @Override
    public Object getBookingCountByDate(LocalDateTime date, String type, int  userId) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDate;
        LocalDateTime endDate;
        if ("1".equals(type)) { //查询当天
            startDate = LocalDateTimeUtils.getDayStart(date);
            endDate = LocalDateTimeUtils.getDayEnd(date);
        } else {//查询当前时间之前的
            startDate = LocalDateTime.parse("1970-01-01 00:00:00", df);
            endDate = LocalDateTimeUtils.getDayEnd(date);
        }

        int t = bizBookingClientMapper.getBookingCountByDate(startDate.format(df), endDate.format(df), userId);
        return t;
    }
}